package service

import (
	"context"
	__ "goods_srv/basic/goodsrpc"
	"goods_srv/handler/dao"
)

type Server struct {
	__.UnimplementedGoodsServer
}

// 商品运费
func (s *Server) CalculateShippingFee(ctx context.Context, in *__.CalculateShippingFeeReq) (*__.CalculateShippingFeeResp, error) {
	res := dao.CalculateShippingFee(float64(in.Weight), float64(in.WayLong))
	return &__.CalculateShippingFeeResp{Fee: float32(res)}, nil
}

// 创建订单
func (s *Server) CreateGoodsOrder(ctx context.Context, in *__.CreateGoodsOrderReq) (*__.CreateGoodsOrderResp, error) {
	res, err := dao.CreateOrder(in)
	if err != nil {
		return nil, err
	}
	return &__.CreateGoodsOrderResp{
		Id:      res.Id,
		OrderNo: res.OrderNo,
	}, nil
}

// 发货
func (s *Server) BatchShipping(ctx context.Context, in *__.BatchShippingReq) (*__.BatchShippingResp, error) {
	err := dao.BatchShipping(in)
	if err != nil {
		return nil, err
	}
	return &__.BatchShippingResp{Res: true}, nil
}

// 商品详情
func (s *Server) GoodsDetail(ctx context.Context, in *__.GoodsDetailReq) (*__.GoodsDetailResp, error) {
	g, err := dao.GoodsDetail(in.Id)
	if err != nil {
		return nil, err
	}
	return &__.GoodsDetailResp{
		Id:          g.Id,
		GoodsName:   g.GoodsName,
		GoodsPrice:  float32(g.GoodsPrice),
		BrandName:   g.BrandName,
		GoodsUrl:    g.GoodsUrl,
		Sales:       g.Sales,
		GoodsDesc:   g.GoodsDesc,
		GoodsStock:  g.GoodsStock,
		GoodsStatus: uint32(g.GoodsStatus),
	}, nil
}

// 订单详情
func (s *Server) OrderDetail(ctx context.Context, in *__.OrderDetailReq) (*__.OrderDetailResp, error) {
	o, err := dao.OrderDetail(in.OrderId)
	if err != nil {
		return nil, err
	}
	return &__.OrderDetailResp{
		Id:          o.Id,
		OrderNo:     o.OrderNo,
		AddressId:   o.AddressId,
		UserId:      o.UserId,
		GoodId:      o.GoodId,
		ShopId:      o.ShopId,
		GoodsNum:    o.GoodsNum,
		GoodsName:   o.GoodsName,
		GoodsPrice:  o.GoodsPrice,
		OrderPrice:  float32(o.OrderPrice),
		PayCType:    o.PayCType,
		PayStatus:   o.PayStatus,
		OrderStatus: o.OrderStatus,
		Address:     o.Address,
		ShopName:    o.ShopName,
		CreateAt:    o.CreatedAt.Format("2006-01-02 15:04:05"),
	}, nil
}
